//
//  ZJDiscussAddView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/9/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJDiscussAddView.h"
#import "ZJDiscussViwe.h"
@interface ZJDiscussAddView ()
{
    NSArray *_array;
}
/** 分割线 */
@property (nonatomic, strong) UILabel *spaceLine;
/** scrollview */
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation ZJDiscussAddView

- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        _array = array;
        [self addSubview:self.spaceLine];
        [self addSubview:self.scrollView];
        [self setupUI];
    }
    return self;
}

- (void)setupUI{

    
    CGFloat sepace = 25 * [UIScreen zj_screenWScale];
    CGFloat width = ([UIScreen zj_screenWith] - 5 * sepace) / 4;
    CGFloat height = 75 * [UIScreen zj_screenHScale];
    
    for (int i = 0; i < _array.count; i ++) {
        int zheng = i / 4;
        int yu = i % 4;
        ZJDiscussViwe *view = [[ZJDiscussViwe alloc] initWithFrame:CGRectMake(sepace + (sepace + width) * yu, sepace + (height + sepace) * zheng, width, height) dict:_array[i]];
        view.tag = i;
        UITapGestureRecognizer *tapGester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_ViewClick:)];
        [view addGestureRecognizer:tapGester];
        [self.scrollView addSubview:view];
        if (i == 2) {
            self.summartoyView = view;
        }
        self.scrollView.contentSize = CGSizeMake(self.width, view.bottom + sepace);
        if (self.scrollView.contentSize.height >= self.scrollView.height) {
            self.scrollView.scrollEnabled = YES;
        }
    }
}

- (void)action_ViewClick:(UITapGestureRecognizer *)tapGester{
    NSInteger tag = tapGester.view.tag;
    if (self.delegate) {
        [self.delegate ZJDiscussAddViewDelegate_viewClickTag:tag];
    }
}

#pragma mark - Setter
- (void)setData:(NSArray *)data{
    _data = data;
    _array = data;
    for (ZJDiscussViwe *discuss in self.scrollView.subviews) {
        
        if ([discuss isKindOfClass:[ZJDiscussViwe class]]) {
            [discuss removeFromSuperview];
        }
    }
    
    
    CGFloat sepace = 25 * [UIScreen zj_screenWScale];
    CGFloat width = ([UIScreen zj_screenWith] - 5 * sepace) / 4;
    CGFloat height = 75 * [UIScreen zj_screenHScale];
    
    for (int i = 0; i < _array.count; i ++) {
        int zheng = i / 4;
        int yu = i % 4;
        ZJDiscussViwe *view = [[ZJDiscussViwe alloc] initWithFrame:CGRectMake(sepace + (sepace + width) * yu, sepace + (height + sepace) * zheng, width, height) dict:_array[i]];
        view.tag = i;
        UITapGestureRecognizer *tapGester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_ViewClick:)];
        [view addGestureRecognizer:tapGester];
        [self.scrollView addSubview:view];
        self.scrollView.contentSize = CGSizeMake(self.width, view.bottom + sepace);
        if (self.scrollView.contentSize.height >= self.scrollView.height) {
            self.scrollView.scrollEnabled = YES;
        }
    }
}

#pragma mark - Getters

- (UIScrollView *)scrollView{
    if (_scrollView == nil){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 1, self.width, self.height - 1)];
        _scrollView.contentOffset = CGPointZero;
        _scrollView.scrollEnabled = NO;
    }
    return _scrollView;
}
- (UILabel *)spaceLine{
    if (_spaceLine == nil){
        _spaceLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 1)];
        _spaceLine.backgroundColor = [UIColor themeGrayColor];
        
    }
    return _spaceLine;
}
@end
